package com.ktwlsoft.archivemanage.domain.business.repositoryService.businessInspect;

import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.UnitMember;
import com.ktwlsoft.archivemanage.domain.dao.entity.businessInspect.Comment;
import com.ktwlsoft.archivemanage.domain.dao.repository.IAccountMapper;
import com.ktwlsoft.archivemanage.domain.dao.repository.UnitMemberMapper;
import com.ktwlsoft.archivemanage.domain.dao.repository.businessInspect.ICommentMapper;
import com.ktwlsoft.archivemanage.payload.businessGuidance.CommentRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 评论相关服务
 */
@Service
public class CommentRepository {

    @Autowired
    private ICommentMapper mapper;

    @Autowired
    private IAccountMapper accountMapper;

    @Autowired
    private UnitMemberMapper unitMemberMapper;

    @Transactional(rollbackFor = Exception.class)
    public int saveComment(CommentRequest request) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(request, comment);
        Account account = accountMapper.getById(request.getUserId());
        if(account != null){
            if ("立档单位".equals(account.getRoleName())) {
                UnitMember unitMember = unitMemberMapper.findUnitMemberByArchivistId(request.getUserId());
                comment.setUserProfile(unitMember.getProfile());
            }else{
                comment.setUserProfile(account.getProfile());
            }
        }
        return mapper.insert(comment);
    }

    public List<Comment> getAll(Long guidanceId) {
        return mapper.getAll(guidanceId);
    }

    public int updateComment(CommentRequest request) {
        Comment comment = new Comment();
        comment.setId(Long.parseLong(request.getId()));
        return mapper.update(comment);
    }

    public int delete(Long id) {
        return mapper.delete(id);
    }
}
